package com.xmy.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        MyCallable callable = new MyCallable();
        //适配类FutureTask<T>,本质上就是一个Runnable
        FutureTask<Integer> futureTask = new FutureTask<>(callable);
        //向Thread()中传入FutureTask对象，启动线程
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();
        Integer s = futureTask.get();//获取Callable的返回结果
        System.out.println(s);

    }
}

class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"=>call");
        return 1024;
    }
}
